<template>
	<view>
		<view v-if="list.length>0">
			<uni-list :border="true" v-for="item in list">

				<uni-list-chat :title="item.groupName" :avatar="path+item.avatarBytes"
					:note="item.badgeText>0?'您有新的申请':''" clickable badge-positon="left" :badge-text="item.badgeText"
					link="navigateTo"
					:to="'/pages/examine-list/index?receptionUserId='+item.id+'&groupId='+item.groupId">
					<view class="chat-custom-right">
						<uni-icons type="right" color="#999" size="18"></uni-icons>
					</view>
				</uni-list-chat>
			</uni-list>
		</view>
		<view v-else>
			<no-data />
		</view>
	</view>
</template>

<script>
	import {
		request,
		baseURL
	} from "@/api/request.js"

	import {
		getGroupList,
		getGroupName
	} from "../../utils/common"
	import noData from "../../component/no-data.vue"
	export default {
		data() {
			return {
				list: [],
				path: baseURL + '/api/v1/common/download/',
			}
		},
		components: {
			'no-data': noData
		},
		onLoad() {
			this.getList()
		},
		onShow() {
			this.getList()
		},
		methods: {
			getList() {
				let openid = wx.getStorageSync('openId')
				request({
					url: `/api/v1/people-info/openid?openid=${openid}`,
					method: 'GET'
				}).then(res => {
					let list = res.data;
					getGroupList('').then(res => {
						this.list = list.filter(item => {
							item.groupName = getGroupName(res, item.groupId);
							return item.status == 2 && item.type == 1
						})

						this.list.forEach(item => {
							this.getBadgeText(item.groupId, item.id).then(to => {
								console.log(to);
								item.badgeText = to;
							});
						})
					})
				})
			},
			async getBadgeText(groupId, senseId) {
				let total = 0
				await request({
					url: '/api/v1/people-info/guest',
					method: 'POST',
					data: {
						type: 2,
						current: 1,
						size: 100,
						status: 0,
						receptionUserId: senseId,
						groupId,
					}
				}).then(res => {
					total = res.data.total
				})
				return total
			}
		}
	}
</script>

<style>
	.time,
	.motive {
		width: 98%;
		color: #ccc;
		font-size: 12px;
		white-space: pre-wrap;
		overflow-wrap: break-word;
	}
</style>